package cn.yuxirey.verifyedittext;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

/**
 * @date 2022/3/14 15:22
 */
public class OperationsPopupWindow extends PopupWindow {
    private OnPasteCallback mPasteCallback;

    public OperationsPopupWindow(Context context) {
        this(context, null);
    }

    public OperationsPopupWindow(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public OperationsPopupWindow(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public OperationsPopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setBackgroundDrawable(null);
        setWidth(toPx(context, 56));
        setHeight(toPx(context, 32));
        setOutsideTouchable(true);
        initView(context);
    }

    private void initView(Context context) {
        TextView tvPaste = new TextView(context);
        tvPaste.setPadding(toPx(context, 8),
                toPx(context, 2),
                toPx(context, 8),
                toPx(context, 2));
        tvPaste.setText(R.string.vc_paste);
        tvPaste.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f);
        tvPaste.setGravity(Gravity.CENTER);
        tvPaste.setTextColor(Color.parseColor("#333333"));

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        tvPaste.setLayoutParams(params);
        setContentView(tvPaste);
        ShadowDrawable.setShadowDrawable(tvPaste,
                Color.WHITE,
                0,
                Color.parseColor("#40000000"),
                toPx(context, 4),
                0,
                0);

        tvPaste.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  PASTE. on 2022/3/14 15:30.
                doPaste();
                dismiss();
            }
        });
    }

    public interface OnPasteCallback {
        void onPaste(OperationsPopupWindow popupWindow, String text);
    }

    public void setPasteCallback(@Nullable OnPasteCallback callback) {
        this.mPasteCallback = callback;
    }

    private int toPx(Context context, int dp) {
        return (int) (dp * context.getResources().getDisplayMetrics().density + 0.5f);
    }

    private void doPaste() {
        if (mPasteCallback == null) {
            return;
        }
        ClipboardManager clipboardManager = (ClipboardManager) getContentView().getContext().getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboardManager == null) {
            return;
        }
        ClipData clipData = clipboardManager.getPrimaryClip();
        if (clipData == null || clipData.getItemCount() == 0) {
            return;
        }
        String text = clipData.getItemAt(0).getText().toString();
        if (TextUtils.isEmpty(text) || !TextUtils.isDigitsOnly(text)) {
            return;
        }
        mPasteCallback.onPaste(OperationsPopupWindow.this, text);
    }
}
